package com.example.shushiyunsst.fragment.main.mine.child

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.mine.child.bean.HelperCenterBean
import com.example.shushiyunsst.fragment.main.mine.child.bean.MessageBean
import com.example.shushiyunsst.util.MESSAGE_PAGE
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

@Route(path = MESSAGE_PAGE)
class MessageFragment : SimpleListFragment<MessageBean>() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration=false
    }

//    private var pos=-1

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        val mToolbar:Toolbar =mHeader.findViewById(R.id.mToolbar)
        mToolbar.title="公告"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun getItemLayout(): Int {
        return R.layout.message_fragment
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: MessageBean?) {
        helper?.setText(R.id.title,item?.title)
            ?.setText(R.id.time,item?.createTime)
//            ?.setText(R.id.content,item?.content)
//            ?.setViewVisible(R.id.content,pos==helper.layoutPosition)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(MessageBean::class.java).endSubType().build()
    }

    override fun netRequest() {
//        val str=""
//        parseDate(str)
        val map = mapOf(
            "pageCount" to Int.MAX_VALUE,
            "pageIndex" to pageIndex
        )
        RestClient.builder()
            .url(R.string.queryNewsList)
            .raw(JSON.toJSONString(map))
            .success { parseDate(it) }
            .failure { onFailed() }
            .error { _, _ -> onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val parse = JSON.parseObject(data)
        val list = parse["list"]
        val listStr = list?.toString() ?: ""
        val array = JSON.parseArray(listStr, MessageBean::class.java)
        loadListDate(array)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        super.onItemClick(adapter, view, position)
        val item = mAdapter.getItem(position)
        start(MessageSecondFragment.newInstance(item))
//        pos=position
//        mAdapter.notifyDataSetChanged()
    }
}